package com.training.base;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

/**
 * 响应工具类，用于统一处理ResponseEntity返回
 */
@Component
public class ResponseUtils {

    @Autowired
    private Gson gson;

    /**
     * 返回成功的响应
     *
     * @param data 数据
     * @param <T>  数据类型
     * @return ResponseEntity
     */
    public <T> ResponseEntity<Result<T>> success(T data) {
        Result<T> result = Result.success(data);
        return ResponseEntity.ok(result);
    }

    /**
     * 返回成功的响应，带自定义消息
     *
     * @param data    数据
     * @param message 消息
     * @param <T>     数据类型
     * @return ResponseEntity
     */
    public <T> ResponseEntity<Result<T>> success(T data, String message) {
        Result<T> result = new Result<>(data, true, message);
        return ResponseEntity.ok(result);
    }

    /**
     * 返回失败的响应
     *
     * @param message 错误消息
     * @return ResponseEntity
     */
    public ResponseEntity<Result<Object>> fail(String message) {
        Result<Object> result = Result.fail(message);
        return ResponseEntity.status(500).body(result);
    }

    /**
     * 返回失败的响应，带自定义状态码
     *
     * @param code    状态码
     * @param message 错误消息
     * @return ResponseEntity
     */
    public ResponseEntity<Result<Object>> fail(int code, String message) {
        Result<Object> result = Result.fail(code, message);
        return ResponseEntity.status(code == 401 || code == 403 ? 401 : code).body(result);
    }

    /**
     * 返回失败的响应，带自定义状态码和数据
     *
     * @param code    状态码
     * @param message 错误消息
     * @param data    数据
     * @return ResponseEntity
     */
    public ResponseEntity<Result<Object>> fail(int code, String message, Object data) {
        Result<Object> result = Result.fail(code, message, data);
        return ResponseEntity.status(code == 401 || code == 403 ? 401 : code).body(result);
    }

    /**
     * 返回指定类型的失败响应
     *
     * @param code    状态码
     * @param message 错误消息
     * @param <T>     数据类型
     * @return ResponseEntity
     */
    public <T> ResponseEntity<Result<T>> failWithType(int code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(null);
        return ResponseEntity.status(code == 401 || code == 403 ? 401 : code).body(result);
    }

    /**
     * 返回自定义状态码的响应
     *
     * @param statusCode HTTP状态码
     * @param data       数据
     * @param <T>        数据类型
     * @return ResponseEntity
     */
    public <T> ResponseEntity<Result<T>> customStatus(int statusCode, T data) {
        Result<T> result = Result.success(data);
        return ResponseEntity.status(statusCode).body(result);
    }

    /**
     * 返回自定义状态码的响应
     *
     * @param statusCode HTTP状态码
     * @param code       业务状态码
     * @param message    消息
     * @param data       数据
     * @param <T>        数据类型
     * @return ResponseEntity
     */
    public <T> ResponseEntity<Result<T>> customStatus(int statusCode, int code, String message, T data) {
        Result<T> result = new Result<>(code, message, data);
        return ResponseEntity.status(statusCode).body(result);
    }
}